library(cjoint);library(haven);library(ggplot2);library(car);library(cregg)
setwd("~/Dropbox/GenderJudgingExperiment")

respondent_level <- read.csv("Data/KNH_GenderConjoint_Prolific.csv")
table(respondent_level$gender)
sum(table(respondent_level$ideo)[1:3])/sum(table(respondent_level$ideo))
sum(table(respondent_level$polinterest)[1:2])/sum(table(respondent_level$polinterest))
sum(table(respondent_level$votefreq)[1:2])/sum(table(respondent_level$votefreq))

csv.to.clean <- read.csv("Data/KNH_GenderConjoint_Prolific.csv")
mean(as.numeric(csv.to.clean$Q_TotalDuration[3*32]))/60

csv.to.clean1 <- csv.to.clean[, !(colnames(csv.to.clean) %in% c("F.1.6","F.1.7",
                                                                "F.2.6","F.2.7",
                                                                "F.3.6","F.3.7",
                                                                "F.4.6","F.4.7",
                                                                "F.5.6","F.5.7",
                                                                "F.6.6","F.6.7",
                                                                "F.7.6","F.7.7",
                                                                "F.8.6","F.8.7",
                                                                "F.9.6","F.9.7",
                                                                "F.10.6","F.10.7",
                                                                "F.1.1.6","F.1.2.6",
                                                                "F.1.1.7","F.1.2.7",
                                                                "F.2.1.6","F.2.2.6",
                                                                "F.2.1.7","F.2.2.7",
                                                                "F.3.1.6","F.3.2.6",
                                                                "F.3.1.7","F.3.2.7",
                                                                "F.4.1.6","F.4.2.6",
                                                                "F.4.1.7","F.4.2.7",
                                                                "F.5.1.6","F.5.2.6",
                                                                "F.5.1.7","F.5.2.7",
                                                                "F.6.1.6","F.6.2.6",
                                                                "F.6.1.7","F.6.2.7",
                                                                "F.7.1.6","F.7.2.6",
                                                                "F.7.1.7","F.7.2.7",
                                                                "F.8.1.6","F.8.2.6",
                                                                "F.8.1.7","F.8.2.7",
                                                                "F.9.1.6","F.9.2.6",
                                                                "F.9.1.7","F.9.2.7",
                                                                "F.10.1.6","F.10.2.6",
                                                                "F.10.1.7","F.10.2.7",
                                                                "F.1.8","F.1.9",
                                                                "F.2.8","F.2.9",
                                                                "F.3.8","F.3.9",
                                                                "F.4.8","F.4.9",
                                                                "F.5.8","F.5.9",
                                                                "F.6.8","F.6.9",
                                                                "F.7.8","F.7.9",
                                                                "F.8.8","F.8.9",
                                                                "F.9.8","F.9.9",
                                                                "F.10.8","F.10.9",
                                                                "F.1.1.8","F.1.2.8",
                                                                "F.1.1.9","F.1.2.9",
                                                                "F.2.1.8","F.2.2.8",
                                                                "F.2.1.9","F.2.2.9",
                                                                "F.3.1.8","F.3.2.8",
                                                                "F.3.1.9","F.3.2.9",
                                                                "F.4.1.8","F.4.2.8",
                                                                "F.4.1.9","F.4.2.9",
                                                                "F.5.1.8","F.5.2.8",
                                                                "F.5.1.9","F.5.2.9",
                                                                "F.6.1.8","F.6.2.8",
                                                                "F.6.1.9","F.6.2.9",
                                                                "F.7.1.8","F.7.2.8",
                                                                "F.7.1.9","F.7.2.9",
                                                                "F.8.1.8","F.8.2.8",
                                                                "F.8.1.9","F.8.2.9",
                                                                "F.9.1.8","F.9.2.8",
                                                                "F.9.1.9","F.9.2.9",
                                                                "F.10.1.8","F.10.2.8",
                                                                "F.10.1.9","F.10.2.9",
                                                                "F.1.10","F.1.11",
                                                                "F.2.10","F.2.11",
                                                                "F.3.10","F.3.11",
                                                                "F.4.10","F.4.11",
                                                                "F.5.10","F.5.11",
                                                                "F.6.10","F.6.11",
                                                                "F.7.10","F.7.11",
                                                                "F.8.10","F.8.11",
                                                                "F.9.10","F.9.11",
                                                                "F.10.10","F.10.11",
                                                                "F.1.1.10","F.1.2.10",
                                                                "F.1.1.11","F.1.2.11",
                                                                "F.2.1.10","F.2.2.10",
                                                                "F.2.1.11","F.2.2.11",
                                                                "F.3.1.10","F.3.2.10",
                                                                "F.3.1.11","F.3.2.11",
                                                                "F.4.1.10","F.4.2.10",
                                                                "F.4.1.11","F.4.2.11",
                                                                "F.5.1.10","F.5.2.10",
                                                                "F.5.1.11","F.5.2.11",
                                                                "F.6.1.10","F.6.2.10",
                                                                "F.6.1.11","F.6.2.11",
                                                                "F.7.1.10","F.7.2.10",
                                                                "F.7.1.11","F.7.2.11",
                                                                "F.8.1.10","F.8.2.10",
                                                                "F.8.1.11","F.8.2.11",
                                                                "F.9.1.10","F.9.2.10",
                                                                "F.9.1.11","F.9.2.11",
                                                                "F.10.1.10","F.10.2.10",
                                                                "F.10.1.11","F.10.2.11"))]
write.csv(csv.to.clean1,"Data/KNH_ProlificCleaned.csv", row.names=FALSE)

##Read In Data
data <- read.qualtrics("Data/KNH_ProlificCleaned.csv", 
                                responses=c("choice_1","choice_2","choice_3","choice_4","choice_5",
                                            "choice_6","choice_7","choice_8","choice_9","choice_10"), 
                                covariates=c("yearborn","gender","degree","polinterest","ideo","pid1","pid4","sexism_1","sexism_2","sexism_3","sexism_4","sexism_5"), respondentID="ResponseId",new.format=TRUE)
names(data)
dim(data)
table(data$gender)
table(data$yearborn)

data_rate <- read.qualtrics("Data/KNH_ProlificCleaned.csv", 
                            ranks=c("qual1_1","qual2_1","qual1_2","qual2_2","qual1_3",
                                        "qual2_3","qual1_4","qual2_4","qual1_5","qual2_5",
                                    "qual1_6","qual2_6","qual1_7","qual2_7","qual1_8",
                                    "qual2_8","qual1_9","qual2_9","qual1_10","qual2_10"), 
                            covariates=c("yearborn","gender","degree","polinterest","ideo","pid1","pid4","sexism_1","sexism_2","sexism_3","sexism_4","sexism_5"), respondentID="ResponseId",new.format=TRUE)
dim(data_rate)
names(data_rate)

data_rate$rating <- 5-data_rate$selected
dim(data_rate)
names(data_rate)
table(data_rate$rating)
table(data$selected)

##Compute Correlation between outcome variables
merged <- merge(data,data_rate,by=c("Response.ID","task","profile"))
dim(data)
dim(data_rate)
dim(merged)
names(merged)
##Check merge was successful
table(merged$Newspaper.Endorsement.rowpos.x,merged$Newspaper.Endorsement.rowpos.y)
table(data_rate$rating)
table(merged$rating)
table(data$selected)
table(merged$selected.x)
cor(merged$selected.x,merged$rating,use="pairwise")
t.test(subset(merged,selected.x==1)$rating,subset(merged,selected.x==0)$rating)

##Create Demographic Variables
data$Female <- -1
data$Female[data$gender==1] <- 0
data$Female[data$gender==2] <- 1
data$Female[data$gender==3] <- NA
data$Female[data$gender==4] <- NA
data$Female[data$gender==5] <- NA
table(data$Female)
data$Female <- factor(data$Female,labels=c("Male","Female"),levels=c(0,1))
prop.table(table(data$Female))

data_rate$Female <- -1
data_rate$Female[data_rate$gender==1] <- 0
data_rate$Female[data_rate$gender==2] <- 1
data_rate$Female[data_rate$gender==3] <- NA
data_rate$Female[data_rate$gender==4] <- NA
data_rate$Female[data_rate$gender==5] <- NA
table(data_rate$Female)
data_rate$Female <- factor(data_rate$Female,labels=c("Male","Female"),levels=c(0,1))
prop.table(table(data_rate$Female))

data$PID <- -1
data$PID[data$pid1==1] <- 1
data$PID[data$pid1==2] <- 3
data$PID[data$pid1==3] <- 2
data$PID[data$pid1==4] <- 2
data$PID[data$pid4==1] <- 3
data$PID[data$pid4==2] <- 1
data$PID[data$pid4==3] <- 2
table(data$PID)
data$PID <- factor(data$PID,labels=c("Democrat","Independent","Republican"),levels=c(1,2,3))
prop.table(table(data$PID))

data_rate$PID <- -1
data_rate$PID[data_rate$pid1==1] <- 1
data_rate$PID[data_rate$pid1==2] <- 3
data_rate$PID[data_rate$pid1==3] <- 2
data_rate$PID[data_rate$pid1==4] <- 2
data_rate$PID[data_rate$pid4==1] <- 3
data_rate$PID[data_rate$pid4==2] <- 1
data_rate$PID[data_rate$pid4==3] <- 2
table(data_rate$PID)
data_rate$PID <- factor(data_rate$PID,labels=c("Democrat","Independent","Republican"),levels=c(1,2,3))
table(data_rate$PID)

data$interest <- -1
data$interest[data$polinterest==1] <- 1
data$interest[data$polinterest==2] <- 1
data$interest[data$polinterest==3] <- 0
data$interest[data$polinterest==4] <- 0
table(data$interest)
data$interest <- factor(data$interest,labels=c("Low Interest","High Interest"),levels=c(0,1))
table(data$interest)

data_rate$interest <- -1
data_rate$interest[data_rate$polinterest==1] <- 1
data_rate$interest[data_rate$polinterest==2] <- 1
data_rate$interest[data_rate$polinterest==3] <- 0
data_rate$interest[data_rate$polinterest==4] <- 0
table(data_rate$interest)
data_rate$interest <- factor(data_rate$interest,labels=c("Low Interest","High Interest"),levels=c(0,1))
table(data_rate$interest)

data$EdAttainment <- -1
data$EdAttainment[data$degree<7] <- 0
data$EdAttainment[data$degree>6] <- 1
table(data$EdAttainment)
data$EdAttainment <- factor(data$EdAttainment,labels=c("No College Degree","College Degree"),levels=c(0,1))
prop.table(table(data$EdAttainment))

data_rate$EdAttainment <- -1
data_rate$EdAttainment[data$degree<7] <- 0
data_rate$EdAttainment[data$degree>6] <- 1
table(data_rate$EdAttainment)
data_rate$EdAttainment <- factor(data_rate$EdAttainment,labels=c("No College Degree","College Degree"),levels=c(0,1))
table(data_rate$EdAttainment)

data_rate <- na.omit(data_rate)
data <- na.omit(data)

table(data$Prior.Political.Experience)

levels(data$Newspaper.Endorsement)[levels(data$Newspaper.Endorsement) == "A strong-minded attorney who will bring leadership to our state’s highest court"] <- "A strong-minded attorney who will bring leadership to our state's highest court"
levels(data_rate$Newspaper.Endorsement)[levels(data_rate$Newspaper.Endorsement) == "A strong-minded attorney who will bring leadership to our state’s highest court"] <- "A strong-minded attorney who will bring leadership to our state's highest court"

data$Education <- factor(data$Education,
                         levels=c("Graduated from in-state public law school",
                                  "Graduated from in-state private law school",
                                  "Graduated from out-of-state public law school",
                                  "Graduated from out-of-state private law school",
                                  "Graduated from Ivy League law school"))
data$Gender <- factor(data$Gender,levels=c("Male","Female"))
data$Newspaper.Endorsement <- factor(data$Newspaper.Endorsement,
                         levels=c("Someone who is extremely well qualified to serve on the state supreme court",
                                  "A dedicated attorney whose tireless work ethic will benefit our judiciary and our state",
                                  "This is an opportunity to elect a caring, compassionate lawyer to our state supreme court",
                                  "A strong-minded attorney who will bring leadership to our state's highest court",
                                  "A true legal expert who will bring expansive legal knowledge to the state supreme court"))
data$Occupation <- factor(data$Occupation,
                         levels=c("Trial court judge",
                                  "County prosecutor",
                                  "Public defender",
                                  "Owns own solo law firm",
                                  "Attorney in private practice specializing in family law",
                                  "Attorney in private practice specializing in corporate mergers and acquisitions"))
data$Prior.Political.Experience <- factor(data$Prior.Political.Experience,
                         levels=c("No political experience",
                                  "City Council Member",
                                  "State Legislator",
                                  "Majority party leader in state legislature",
                                  "Senior legal counsel to the governor"))
data_rate$Education <- factor(data_rate$Education,
                         levels=c("Graduated from in-state public law school",
                                  "Graduated from in-state private law school",
                                  "Graduated from out-of-state public law school",
                                  "Graduated from out-of-state private law school",
                                  "Graduated from Ivy League law school"))
data_rate$Gender <- factor(data_rate$Gender,levels=c("Male","Female"))
data_rate$Newspaper.Endorsement <- factor(data_rate$Newspaper.Endorsement,
                                     levels=c("Someone who is extremely well qualified to serve on the state supreme court",
                                              "A dedicated attorney whose tireless work ethic will benefit our judiciary and our state",
                                              "This is an opportunity to elect a caring, compassionate lawyer to our state supreme court",
                                              "A strong-minded attorney who will bring leadership to our state's highest court",
                                              "A true legal expert who will bring expansive legal knowledge to the state supreme court"))
data_rate$Occupation <- factor(data_rate$Occupation,
                          levels=c("Trial court judge",
                                   "County prosecutor",
                                   "Public defender",
                                   "Owns own solo law firm",
                                   "Attorney in private practice specializing in family law",
                                   "Attorney in private practice specializing in corporate mergers and acquisitions"))
data_rate$Prior.Political.Experience <- factor(data_rate$Prior.Political.Experience,
                                          levels=c("No political experience",
                                                   "City Council Member",
                                                   "State Legislator",
                                                   "Majority party leader in state legislature",
                                                   "Senior legal counsel to the governor"))


conjoint_theme <- theme(panel.border = element_rect(colour = "black",fill=NA,size=1.25),
                        panel.background = element_rect(fill = "white",
                                                        colour = "white",
                                                        size = 0.5, linetype = "solid"),
                        panel.grid.major = element_line(size = 0.5, linetype = 'solid',
                                                        colour = "grey80"), 
                        panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
                                                        colour = "grey80"),
                        legend.position="none",
                        strip.background = element_rect(colour="black", fill=NA,size=1.25),
                        strip.text.x = element_text(
                          size = 14, color = "black", face = "bold"),
                        axis.text.y=element_text(hjust=0),
                        axis.text=element_text(colour="black"),
                        axis.title = element_text(size = 14))

attr(data$Newspaper.Endorsement, "label") <- "Newspaper Endorsement"
attr(data$Prior.Political.Experience, "label") <- "Prior Political Experience"

################
##Overall Results: Candidate Selection Outcome
results <- cjoint::amce(selected ~ Gender+Education+Newspaper.Endorsement+Occupation+Prior.Political.Experience, 
                data=data,respondent.id="Response.ID")

#Table A1
summary(results)

#Figure 1
pdf("Figures/Prolific_Selected.pdf",width=11,height=6,paper='special')
plot(results,plot.theme=conjoint_theme,xlab="Change in Likelihood of Selection",attribute.names=c("Education","Gender","Newspaper Endorsement","Occupation","Prior Political Experience"))
dev.off()

#Figure A1
form <- selected ~ Gender+Education+Newspaper.Endorsement+Occupation+Prior.Political.Experience
results_mm <- mm(form, data=data, id = ~Response.ID)
pdf("Figures/Prolific_Selected_mm.pdf",width=11,height=6,paper='special')
plot(results_mm, vline = 0.5, theme=conjoint_theme, legend_pos="none")
dev.off()

################
##Overall Results: Candidate Rating Outcome
results_rate <- cjoint::amce(rating ~ Gender+Education+Newspaper.Endorsement+Occupation+Prior.Political.Experience, 
                data=data_rate,respondent.id="Response.ID")

#Table A2
summary(results_rate)

#Figure 2
pdf("Figures/Prolific_Rating.pdf",width=11,height=6,paper='special')
plot(results_rate,plot.theme=conjoint_theme,xlab="Change in Qualification Rating",attribute.names=c("Education","Gender","Newspaper Endorsement","Occupation","Prior Political Experience"))
dev.off()

#Figure A2
form <- rating ~ Gender+Education+Newspaper.Endorsement+Occupation+Prior.Political.Experience
results_mm <- mm(form, data=data_rate, id = ~Response.ID)
pdf("Figures/Prolific_Rating_mm.pdf",width=11,height=6,paper='special')
plot(results_mm, vline = 0.5, theme=conjoint_theme, legend_pos="none", size=2)
dev.off()

################
##Differences by Gender: Candidate Selection Outcome
results <- cjoint::amce(selected ~ Gender*Education+Gender*Newspaper.Endorsement+Gender*Occupation+Gender*Prior.Political.Experience, 
                data=data,respondent.id="Response.ID")

#Table A3
summary(results)

#Figure 3
pdf("Figures/Prolific_Selected_Interaction.pdf",width=11,height=6,paper='special')
plot(results,plot.theme=conjoint_theme,facet.names = "Gender",xlab="Change in Likelihood of Selection",
     plot.display="interaction")
dev.off()

#Figure A3
diff_mm <- cj(data, selected ~ Education+Newspaper.Endorsement+Occupation+Prior.Political.Experience, id = ~Response.ID, estimate = "mm_diff", by = ~Gender)
pdf("Figures/Prolific_Selected_Interaction_mm.pdf",width=11,height=6,paper='special')
plot(diff_mm, theme=conjoint_theme, legend_pos="none", size=2)
dev.off()

################
##Differences by Gender: Candidate Rating Outcome
results_rate <- cjoint::amce(rating ~ Gender*Education+Gender*Newspaper.Endorsement+Gender*Occupation+Gender*Prior.Political.Experience, 
                     data=data_rate,respondent.id="Response.ID")

#Table A4
summary(results_rate)

#Figure 4
pdf("Figures/Prolific_Rating_Interaction.pdf",width=11,height=6,paper='special')
plot(results_rate,plot.theme=conjoint_theme,facet.names = "Gender",xlab="Change in Qualification Rating",
     plot.display="interaction")
dev.off()

#Figure A4
diff_mm <- cj(data_rate, rating ~ Education+Newspaper.Endorsement+Occupation+Prior.Political.Experience, id = ~Response.ID, estimate = "mm_diff", by = ~Gender)
pdf("Figures/Prolific_Rating_Interaction_mm.pdf",width=11,height=6,paper='special')
plot(diff_mm, theme=conjoint_theme, legend_pos="none", size=2)
dev.off()

################
#Heterogeneous Treatment Effects by Respondent Gender
results <- cjoint::amce(selected ~ Female*Gender+Female*Education+Female*Newspaper.Endorsement+Female*Occupation+Female*Prior.Political.Experience, 
                        data=data,respondent.id="Response.ID",respondent.varying = "Female")

#Figure A5
pdf("Figures/Prolific_Selected_Gender.pdf",width=11,height=8,paper='special')
plot(results,plot.theme=conjoint_theme,xlab="Change in Likelihood of Selection",plot.display="interaction",attribute.names=c("Education","Gender","Newspaper Endorsement","Occupation","Prior Political Experience"))
dev.off()

results_rate <- cjoint::amce(rating ~ Female*Gender+Female*Education+Female*Newspaper.Endorsement+Female*Occupation+Female*Prior.Political.Experience, 
                             data=data_rate,respondent.id="Response.ID",respondent.varying = "Female")

#Figure A6
pdf("Figures/Prolific_Rating_Gender.pdf",width=11,height=8,paper='special')
plot(results_rate,plot.theme=conjoint_theme,xlab="Change in Qualification Rating",plot.display="interaction",attribute.names=c("Education","Gender","Newspaper Endorsement","Occupation","Prior Political Experience"))
dev.off()

################
#Heterogeneous Treatment Effects by Respondent PID
results <- cjoint::amce(selected ~ PID*Gender+PID*Education+PID*Newspaper.Endorsement+PID*Occupation+PID*Prior.Political.Experience, 
                        data=data,respondent.id="Response.ID",respondent.varying = "PID")

#Figure A7
pdf("Figures/Prolific_Selected_PID.pdf",width=12,height=8,paper='special')
plot(results,plot.theme=conjoint_theme,xlab="Change in Likelihood of Selection",plot.display="interaction",attribute.names=c("Education","Gender","Newspaper Endorsement","Occupation","Prior Political Experience"))
dev.off()

results_rate <- cjoint::amce(rating ~ PID*Gender+PID*Education+PID*Newspaper.Endorsement+PID*Occupation+PID*Prior.Political.Experience, 
                     data=data_rate,respondent.id="Response.ID",respondent.varying = "PID")

#Figure A8
pdf("Figures/Prolific_Rating_PID.pdf",width=11,height=8,paper='special')
plot(results_rate,plot.theme=conjoint_theme,xlab="Change in Qualification Rating",plot.display="interaction",attribute.names=c("Education","Gender","Newspaper Endorsement","Occupation","Prior Political Experience"))
dev.off()

###Prep Data for other HTEs
data$sexism_1flip <- 6-as.numeric(data$respondent)
data$sexism_2flip <- 6-as.numeric(data$sexism_2)
data$sexism_3flip <- 6-as.numeric(data$sexism_3)
data$sexism <- as.numeric(data$sexism_1flip) + as.numeric(data$sexism_2flip) + as.numeric(data$sexism_3flip) + as.numeric(data$sexism_4)
data_rate$sexism_1flip <- 6-as.numeric(data_rate$respondent)
data_rate$sexism_2flip <- 6-as.numeric(data_rate$sexism_2)
data_rate$sexism_3flip <- 6-as.numeric(data_rate$sexism_3)
data_rate$sexism <- as.numeric(data_rate$sexism_1flip) + as.numeric(data_rate$sexism_2flip) + as.numeric(data_rate$sexism_3flip) + as.numeric(data_rate$sexism_4)

quantile(data$sexism,c(.333,.667))
data$Sexism <- -1
data$Sexism[data$sexism<9] <- 0
data$Sexism[data$sexism>8 & data$sexism<11] <- 1
data$Sexism[data$sexism>10] <- 2
data$Sexism[data$sexism==""] <- NA
table(data$Sexism)
table(data$Sexism,data$sexism)
data$Sexism <- factor(data$Sexism,labels=c("Low","Moderate","High"),levels=c(0,1,2))
prop.table(table(data$Sexism))

quantile(data_rate$sexism,c(.333,.667))
data_rate$Sexism <- -1
data_rate$Sexism[data_rate$sexism<9] <- 0
data_rate$Sexism[data_rate$sexism>8 & data_rate$sexism<11] <- 1
data_rate$Sexism[data_rate$sexism>10] <- 2
data_rate$Sexism[data_rate$sexism==""] <- NA
table(data_rate$Sexism)
table(data_rate$Sexism,data_rate$sexism)
data_rate$Sexism <- factor(data_rate$Sexism,labels=c("Low","Moderate","High"),levels=c(0,1,2))
prop.table(table(data_rate$Sexism))

data$age <- 2022-as.numeric(data$yearborn)
data_rate$age <- 2022-as.numeric(data_rate$yearborn)

data$Age <- -1
data$Age[data$age<31] <- 0
data$Age[data$age>30 & data$age<45] <- 1
data$Age[data$age>44 & data$age<65] <- 2
data$Age[data$age>64] <- 3
data$Age[data$age==""] <- NA
table(data$Age)
table(data$Age,data$age)
data$Age <- factor(data$Age,labels=c("Under 30","30-44","45-64","65 and Older"),levels=c(0,1,2,3))
prop.table(table(data$Age))

data_rate$Age <- -1
data_rate$Age[data_rate$age<31] <- 0
data_rate$Age[data_rate$age>30 & data_rate$age<45] <- 1
data_rate$Age[data_rate$age>44 & data_rate$age<65] <- 2
data_rate$Age[data_rate$age>64] <- 3
data_rate$Age[data_rate$age==""] <- NA
table(data_rate$Age)
table(data_rate$Age,data_rate$age)
data_rate$Age <- factor(data_rate$Age,labels=c("Under 30","30-44","45-64","65 and Older"),levels=c(0,1,2,3))
prop.table(table(data_rate$Age))

data$Ideology <- -1
data$Ideology[data$ideo<4] <- 0
data$Ideology[data$ideo==4] <- 1
data$Ideology[data$ideo>4] <- 2
data$Ideology[data$ideo==""] <- NA
table(data$Ideology)
table(data$Ideology,data$ideo)
data$Ideology <- factor(data$Ideology,labels=c("Liberal","Moderate","Conservative"),levels=c(0,1,2))
prop.table(table(data$Ideology))

data_rate$Ideology <- -1
data_rate$Ideology[data_rate$ideo<4] <- 0
data_rate$Ideology[data_rate$ideo==4] <- 1
data_rate$Ideology[data_rate$ideo>4] <- 2
data_rate$Ideology[data_rate$ideo==""] <- NA
table(data_rate$Ideology)
table(data_rate$Ideology,data_rate$ideo)
data_rate$Ideology <- factor(data_rate$Ideology,labels=c("Liberal","Moderate","Conservative"),levels=c(0,1,2))
prop.table(table(data_rate$Ideology))

data_rate <- na.omit(data_rate)
data <- na.omit(data)

################
#Heterogeneous Treatment Effects by Respondent Ideology
results <- cjoint::amce(selected ~ Ideology*Gender+Ideology*Education+Ideology*Newspaper.Endorsement+Ideology*Occupation+Ideology*Prior.Political.Experience, 
                        data=data,respondent.id="Response.ID",respondent.varying = "Ideology")

#Figure A9
pdf("Figures/Prolific_Selected_Ideo.pdf",width=13,height=8,paper='special')
plot(results,plot.theme=conjoint_theme,xlab="Change in Likelihood of Selection",plot.display="interaction",attribute.names=c("Education","Gender","Newspaper Endorsement","Occupation","Prior Political Experience"))
dev.off()

results_rate <- cjoint::amce(rating ~ Ideology*Gender+Ideology*Education+Ideology*Newspaper.Endorsement+Ideology*Occupation+Ideology*Prior.Political.Experience, 
                             data=data_rate,respondent.id="Response.ID",respondent.varying = "Ideology")

#Figure A10
pdf("Figures/Prolific_Rating_Ideo.pdf",width=13,height=8,paper='special')
plot(results_rate,plot.theme=conjoint_theme,xlab="Change in Qualification Rating",plot.display="interaction",attribute.names=c("Education","Gender","Newspaper Endorsement","Occupation","Prior Political Experience"))
dev.off()

################
#Heterogeneous Treatment Effects by Respondent's Level of Hostile Sexism
##Four item scale from 
results <- cjoint::amce(selected ~ Sexism*Gender+Sexism*Education+Sexism*Newspaper.Endorsement+Sexism*Occupation+Sexism*Prior.Political.Experience, 
                        data=data,respondent.id="Response.ID",respondent.varying = "Sexism")
#Figure A11
pdf("Figures/Prolific_Selected_Sexism.pdf",width=11,height=8,paper='special')
plot(results,plot.theme=conjoint_theme,xlab="Change in Likelihood of Selection",plot.display="interaction",attribute.names=c("Education","Gender","Newspaper Endorsement","Occupation","Prior Political Experience"))
dev.off()

results_rate <- cjoint::amce(rating ~ Sexism*Gender+Sexism*Education+Sexism*Newspaper.Endorsement+Sexism*Occupation+Sexism*Prior.Political.Experience, 
                             data=data_rate,respondent.id="Response.ID",respondent.varying = "Sexism")
#Figure A12
pdf("Figures/Prolific_Rating_Sexism.pdf",width=11,height=8,paper='special')
plot(results_rate,plot.theme=conjoint_theme,xlab="Change in Qualification Rating",plot.display="interaction",attribute.names=c("Education","Gender","Newspaper Endorsement","Occupation","Prior Political Experience"))
dev.off()

##Table A5 and A6
library(modelsummary)  # Make nice tables and plots for models
library(sandwich)  # Adjust standard errors
library(lmtest)    # Recalculate model errors with sandwich functions with coeftest()

##Gender
summary(lm(selected ~ Female*Gender+Female*Education+Female*Newspaper.Endorsement+Female*Occupation+Female*Prior.Political.Experience, 
           data=data))
summary(lm(rating ~ Female*Gender+Female*Education+Female*Newspaper.Endorsement+Female*Occupation+Female*Prior.Political.Experience, 
           data=data_rate))

round(coeftest(lm(selected ~ Female*Gender+Female*Education+Female*Newspaper.Endorsement+Female*Occupation+Female*Prior.Political.Experience, 
            data=data),
         vcov = vcovCL,
         type = "HC1",
         cluster = ~Response.ID),2)
round(coeftest(lm(rating ~ Female*Gender+Female*Education+Female*Newspaper.Endorsement+Female*Occupation+Female*Prior.Political.Experience, 
         data=data_rate),
         vcov = vcovCL,
         type = "HC1",
         cluster = ~Response.ID),2)


##PID
summary(lm(selected ~ PID*Gender+PID*Education+PID*Newspaper.Endorsement+PID*Occupation+PID*Prior.Political.Experience, 
           data=data))
summary(lm(rating ~ PID*Gender+PID*Education+PID*Newspaper.Endorsement+PID*Occupation+PID*Prior.Political.Experience, 
           data=data_rate))

round(coeftest(lm(selected ~ PID*Gender+PID*Education+PID*Newspaper.Endorsement+PID*Occupation+PID*Prior.Political.Experience, 
            data=data),
         vcov = vcovCL,
         type = "HC1",
         cluster = ~Response.ID),2)
round(coeftest(lm(rating ~ PID*Gender+PID*Education+PID*Newspaper.Endorsement+PID*Occupation+PID*Prior.Political.Experience, 
            data=data_rate),
         vcov = vcovCL,
         type = "HC1",
         cluster = ~Response.ID),2)

##Ideo
summary(lm(selected ~ Ideology*Gender+Ideology*Education+Ideology*Newspaper.Endorsement+Ideology*Occupation+Ideology*Prior.Political.Experience, 
           data=data))
summary(lm(rating ~ Ideology*Gender+Ideology*Education+Ideology*Newspaper.Endorsement+Ideology*Occupation+Ideology*Prior.Political.Experience, 
           data=data_rate))

round(coeftest(lm(selected ~ Ideology*Gender+Ideology*Education+Ideology*Newspaper.Endorsement+Ideology*Occupation+Ideology*Prior.Political.Experience, 
            data=data),
         vcov = vcovCL,
         type = "HC1",
         cluster = ~Response.ID),2)
round(coeftest(lm(rating ~ Ideology*Gender+Ideology*Education+Ideology*Newspaper.Endorsement+Ideology*Occupation+Ideology*Prior.Political.Experience, 
            data=data_rate),
         vcov = vcovCL,
         type = "HC1",
         cluster = ~Response.ID),2)

summary(lm(selected ~ Sexism*Gender+Sexism*Education+Sexism*Newspaper.Endorsement+Sexism*Occupation+Sexism*Prior.Political.Experience, 
           data=data))
summary(lm(rating ~ Sexism*Gender+Sexism*Education+Sexism*Newspaper.Endorsement+Sexism*Occupation+Sexism*Prior.Political.Experience, 
           data=data_rate))

round(coeftest(lm(selected ~ Sexism*Gender+Sexism*Education+Sexism*Newspaper.Endorsement+Sexism*Occupation+Sexism*Prior.Political.Experience, 
            data=data),
         vcov = vcovCL,
         type = "HC1",
         cluster = ~Response.ID),2)
round(coeftest(lm(rating ~ Sexism*Gender+Sexism*Education+Sexism*Newspaper.Endorsement+Sexism*Occupation+Sexism*Prior.Political.Experience, 
            data=data_rate),
         vcov = vcovCL,
         type = "HC1",
         cluster = ~Response.ID),2)
